package service;

import dataAcsessObject.Exception.NotEntityFoundException;
import dataAcsessObject.JPADAOFactory;
import entity.Course;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import service.exception.ObjectNotFoundException;
import valueObject.CategoryVO;
import valueObject.CourseVO;
import valueObject.ProfessorVO;
import valueObject.UniversityVO;

/**
 *
 * @author Juan Hernández Serrato
 */
public class CourseService extends AbstractService<CourseVO, Course> {

    @Override
    public void create(CourseVO valueObject, EntityManager em) {
        JPADAOFactory.getInstance().getCourseDAO().create(valueObject.toEntity(), em);
    }

    @Override
    public void update(CourseVO valueObject, EntityManager em) throws ObjectNotFoundException {
        try {
            Course course = JPADAOFactory.getInstance().getCourseDAO().findById(valueObject.getId(), em);
            course = valueObject.toEntity();
            JPADAOFactory.getInstance().getCourseDAO().update(course, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public void delete(Object id, EntityManager em) throws ObjectNotFoundException {
        try {
            Course course = JPADAOFactory.getInstance().getCourseDAO().findById(id, em);
            JPADAOFactory.getInstance().getCourseDAO().delete(course, em);
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public CourseVO findById(Object id, EntityManager em) throws ObjectNotFoundException {
        try {
            return JPADAOFactory.getInstance().getCourseDAO().findById(id, em).toValueObject();
        } catch (NotEntityFoundException ex) {
            throw new ObjectNotFoundException();
        }
    }

    @Override
    public List<CourseVO> findAll(EntityManager em) {
        List<CourseVO> courseVOs = new ArrayList<CourseVO>();
        List<Course> courses = JPADAOFactory.getInstance().getCourseDAO().findAll(em);
        for (Course course : courses) {
            courseVOs.add(course.toValueObject());
        }
        return courseVOs;
    }

    @Override
    public List<CourseVO> findRange(int begin, int end, EntityManager em) {
        List<CourseVO> courseVOs = new ArrayList<CourseVO>();
        List<Course> courses = JPADAOFactory.getInstance().getCourseDAO().findRange(begin, end, em);
        for (Course course : courses) {
            courseVOs.add(course.toValueObject());
        }
        return courseVOs;
    }
    
    public List<CourseVO> findCourseByCategory(CategoryVO categoryVO, EntityManager em){
        List<CourseVO> courseVOs = new ArrayList<CourseVO>();
        List<Course> courses = JPADAOFactory.getInstance().getCourseDAO().findCoursesByCategoryId(categoryVO.getId(), em);
        for (Course course : courses) {
            courseVOs.add(course.toValueObject());
        }
        return courseVOs;
    }
    
    public List<CourseVO> findCoursesByUniversity(UniversityVO universityVO, EntityManager em){
        List<CourseVO> courseVOs = new ArrayList<CourseVO>();
        List<Course> courses = JPADAOFactory.getInstance().getCourseDAO().findCoursesByUniversity(universityVO.getUserName(), em);
        for (Course course : courses) {
            courseVOs.add(course.toValueObject());
        }
        return courseVOs;
    }
    
    public List<CourseVO> findCoursesByProfessor(ProfessorVO professorVO, EntityManager em){
        List<CourseVO> courseVOs = new ArrayList<CourseVO>();
        List<Course> courses = JPADAOFactory.getInstance().getCourseDAO().findCoursesByProfessor(professorVO.getUserName(), em);
        for (Course course : courses) {
            courseVOs.add(course.toValueObject());
        }
        return courseVOs;
    }
}
